import moment from 'moment'
import Vue from 'vue'

// 第一个是函数名,第二个是函数方法
Vue.filter('dateformat', function (dataStr, pattern = 'YYYY-MM-DD HH:mm:ss') {
  return moment(dataStr).format(pattern)
})

export default {
  data () {
    return {
      goodsList: [],
      pagenum: 1,
      total: 0
    }
  },

  watch: {
    $route (to, from) {
      console.log(to.params.page)
      this.mlLoadGoodsList(to.params.page)
    }
  },

  created () {
    // 获取路劲参数
    const curPage = this.$route.params.page

    this.mlLoadGoodsList(curPage)
  },
  methods: {
    /**
     *  加载数据
     */
    async mlLoadGoodsList (pagenum = 1) {
      const res = await this.$axios.get('/goods', {
        params: {
          query: '',
          pagenum,
          pagesize: 4
        }
      })
      console.log(res)
      this.goodsList = res.data.data.goods
      this.total = res.data.data.total
      this.pagenum = res.data.data.pagenum
    },
    /**
     *  页数改变
     */
    pageChange (curPage) {
      // this.mlLoadGoodsList(curPage)

      // 跳转路由
      this.$router.push(`/goods/${curPage}`)
    }
  }
}
